package com.long1008.controller.salary;

import cn.hutool.core.collection.CollUtil;
import com.long1008.pojo.RewordRules;
import com.long1008.pojo.AllSalary;
import com.long1008.pojo.Employee;
import com.long1008.pojo.SalSearch;
import com.long1008.common.poi.PoiUtils;
import com.long1008.service.EmpService;
import com.long1008.service.RewordRulesService;
import com.long1008.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 薪资查询模块
 *
 */
@RestController
@RequestMapping("/salary/search")
public class SalarySearchController {

    @Autowired
    SalaryService salaryService;

    @Autowired
    EmpService empService;

    @Autowired
    RewordRulesService rewordRulesService;

    @RequestMapping(value = "/exportsalary", method = RequestMethod.GET)
    public ResponseEntity<byte[]> exportEmployeeSalary() {
        List<SalSearch> salSearches = new LinkedList<>();
        List<Employee> employees = empService.getAllEmployees();
        SalSearch salSearch = null;
        RewordRules allRewordRules = rewordRulesService.getAllRewordRules();
        Date date = new Date();
        //查询出来所有的员工id
        List<Long> empIdList = employees.stream().map(Employee::getId).collect(Collectors.toList());
        Map<Long,Integer> allSalaryMap = salaryService.getSalaryByEids(empIdList);
        Map<String, Map<Long, Integer>> overtimeAndLateCountByEids = salaryService.getOvertimeAndLateCountByEids(empIdList);
        Map<Long, Integer> late = overtimeAndLateCountByEids.get("late");
        Map<Long, Integer> overtime = overtimeAndLateCountByEids.get("overtime");
        Map<Long, Integer> leave = overtimeAndLateCountByEids.get("leave");
        for(Employee employee : employees){
            salSearch = new SalSearch();
            // 通过员工eid得到每个员工对应的月薪资
            Long id = employee.getId();
            Integer monthSalary = allSalaryMap.get(id) == null ? 0 : allSalaryMap.get(id);
            salSearch.setEid(id);
            salSearch.setWorkID(employee.getWorkID());
            salSearch.setEmpName(employee.getName());
            salSearch.setMonth(date);
            int overtimeByMonth = 0;
            int lateCountByMonth = 0;
            int leaveCountByMonth = 0;
            if(CollUtil.isNotEmpty(overtime)){
                overtimeByMonth = overtime.get(id) == null ? 0 : overtime.get(id);
            }
            if(CollUtil.isNotEmpty(late)){
                lateCountByMonth = late.get(id) == null ? 0 : late.get(id);
            }
            if(CollUtil.isNotEmpty(leave)){
                leaveCountByMonth = leave.get(id) == null ? 0 : leave.get(id);
            }
            salSearch.setBonus((int) (overtimeByMonth * allRewordRules.getReword()));
            salSearch.setFine((int) (lateCountByMonth * allRewordRules.getPunishment()
                    + leaveCountByMonth * allRewordRules.getLeave()));
            salSearch.setMonthSalary((int) (monthSalary + overtimeByMonth * allRewordRules.getReword()
                    - lateCountByMonth * allRewordRules.getPunishment()
                    - leaveCountByMonth * allRewordRules.getLeave()));
            salSearches.add(salSearch);
        }
        return PoiUtils.exportEmployeeSalaryExcel(salSearches);
    }
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public Map<String, Object> getSalaryByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size){

        Map<String, Object> map = new HashMap<>();
        List<Employee> employeeByPage = empService.getEmployeeByPageShort(page, size);
        Long count = empService.getCountByKeywords("", null, null, null, null, null, null, null);

        List<SalSearch> salSearches=new LinkedList<>();

        for(Employee employee:employeeByPage){
            SalSearch salSearch = new SalSearch();
            // 通过员工eid得到每个员工对应的月薪资
            AllSalary allSalary = salaryService.getSalaryByEid(employee.getId());
            salSearch.setEid(employee.getId());
            salSearch.setWorkID(employee.getWorkID());
            salSearch.setEmpName(employee.getName());
            Date date = new Date();
            salSearch.setMonth(date);
            salSearch.setBonus((int) (salaryService.getOvertimeByMonth(employee.getId(), date)*rewordRulesService.getAllRewordRules().getReword()));
            salSearch.setFine((int) (salaryService.getLateCountByMonth(employee.getId(), date)*rewordRulesService.getAllRewordRules().getPunishment()
                                        + salaryService.getLeaveCountByMonth(employee.getId(), date)*rewordRulesService.getAllRewordRules().getLeave()));
            salSearch.setMonthSalary((int) (allSalary.getMonthSalary() +salaryService.getOvertimeByMonth(employee.getId(), date)*rewordRulesService.getAllRewordRules().getReword()
                    -salaryService.getLateCountByMonth(employee.getId(), date)*rewordRulesService.getAllRewordRules().getPunishment()
                    -salaryService.getLeaveCountByMonth(employee.getId(), date)*rewordRulesService.getAllRewordRules().getLeave()));
            salSearches.add(salSearch);
        }
        map.put("salaries",salSearches);
        map.put("count",count);
        return map;
    }
    // 根据月份筛选出每个员工该月份所得薪资
    @RequestMapping(value = "/month/{month}",method = RequestMethod.GET)
    public Map<String, Object> getSalaryByPage(
            @PathVariable(required = false) Date month,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size){

        Map<String, Object> map = new HashMap<>();
        List<Employee> employeeByPage = empService.getEmployeeByPageShort(page, size);
        Long count = empService.getCountByKeywords("", null, null, null, null, null, null, null);

        List<SalSearch> salSearches=new LinkedList<>();

        for(Employee employee:employeeByPage){
            SalSearch salSearch = new SalSearch();
            // 通过员工eid得到每个员工对应的月薪资
            AllSalary allSalary = salaryService.getSalaryByEid(employee.getId());
            salSearch.setEid(employee.getId());
            salSearch.setWorkID(employee.getWorkID());
            salSearch.setEmpName(employee.getName());
            salSearch.setMonth(month);
            salSearch.setBonus((int) (salaryService.getOvertimeByMonth(employee.getId(), month)*rewordRulesService.getAllRewordRules().getReword()));
            salSearch.setFine((int) (salaryService.getLateCountByMonth(employee.getId(), month)*rewordRulesService.getAllRewordRules().getPunishment()));
            salSearch.setMonthSalary((int) (allSalary.getMonthSalary() + salaryService.getOvertimeByMonth(employee.getId(), month)*rewordRulesService.getAllRewordRules().getReword()-salaryService.getLateCountByMonth(employee.getId(), month)*rewordRulesService.getAllRewordRules().getPunishment()));
            salSearches.add(salSearch);
        }
        map.put("salaries",salSearches);
        map.put("count",count);
        return map;
    }
    // 查询根据薪资分段，各阶段员工人数
    @RequestMapping(value = "/separate",method = RequestMethod.GET)
    public List<Integer> separateBySalary() {
        List<Integer> result = new ArrayList<>();
        Integer part1 = salaryService.separateBySalary12500();
        Integer part2 = salaryService.separateBySalary15000();
        Integer part3 = salaryService.separateBySalary17500();
        Integer part4 = salaryService.separateBySalary20000();
        Integer part5 = salaryService.separateBySalary22500();
        Integer part6 = salaryService.separateBySalary25000();
        result.add(part1);
        result.add(part2);
        result.add(part3);
        result.add(part4);
        result.add(part5);
        result.add(part6);
        return result;
    }
}
